package com.student.foodAfter.controller;

import com.student.foodAfter.common.aop.OperLog;
import com.student.foodAfter.utils.Result;
import com.student.foodAfter.entity.Orderstate;
import com.student.foodAfter.service.OrderStateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderstate")
public class OrderStateController {

    @Resource
    private OrderStateService orderStateService;

    //添加订单记录
    @PostMapping("/addOrder")
    @OperLog(operModul = "订单模块",operType = "增加",operDesc = "添加用户订单记录")
    public Result addOrderState(@RequestBody Object obj){
        Map entity = (Map)obj;
        String username = entity.get("username").toString();
        String sum1 = entity.get("sum").toString();
        int sum = Integer.valueOf(sum1);
        //时间
        Date date = new Date(); SimpleDateFormat dateFormat= new
                SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");


        Orderstate orderstate1 = new Orderstate();
        orderstate1.setUsername(username);
        orderstate1.setMoney(sum);
        orderstate1.setCreatedate(dateFormat.format(date));
        orderstate1.setState("顾客下单");
        boolean b = orderStateService.addOrderState(orderstate1);
        if(b)
            return Result.success("订单状态插入成功");
        else
            return  Result.fail("订单状态插入失败");
    }

    //更改支付状态
    @GetMapping("/updateState/{username}")
    @OperLog(operModul = "订单模块",operType = "修改",operDesc = "修改用户订单状态")
    public Result updateState(@PathVariable String username){
        boolean ls = orderStateService.updateState(username);
        return Result.success("成功",ls);
    }
    //获得支付状态
    @GetMapping("/getState/{username}")
    @OperLog(operModul = "订单模块",operType = "查询",operDesc = "获得订单支付状态")
    public Result getState(@PathVariable String username){
        String state = orderStateService.getState(username);
        if(state.equals("顾客付款"))
            return Result.success("顾客付款","顾客付款");
        else if(state.equals("顾客下单"))
            return Result.success("顾客下单","顾客下单");
        else if(state.equals("商家接单"))
            return Result.success("商家接单","商家接单");
        else if(state.equals("尚未添加"))
            return Result.success("尚未添加","顾客下单");
        else
            return Result.fail("获取状态失败","失败");
    }

    //获得当前用户已完成的订单
    @GetMapping("/getStateList/{username}")
    @OperLog(operModul = "订单模块",operType = "查询",operDesc = "获得用户已完成订单")
    public Result getFishList(@PathVariable String username){
        List<Orderstate> finshOrder = orderStateService.getFinshOrder(username);
        if(finshOrder.size()>0)
            return Result.success("查询成功",finshOrder);
        else
            return Result.success("查询失败",finshOrder);
    }
}
